package com.pug.service.message;

import com.pug.dto.MessageDto;
import com.pug.mapper.message.MessageRepository;
import com.pug.pojo.KssUser;
import com.pug.threadlocal.UserThreadLocal;
import com.pug.vo.MessagePageVo;
import com.pug.vo.Order;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Author: lqm
 * create: 2022/3/28 14:02
 * Lover: lqx
 *
 * @see
 */
@Service
public class MessageService implements IMessageService{
    @Resource
    MessageRepository messageRepository;
    @Resource
    MongoTemplate mongoTemplate;
    //保存信息 messageRepository
    @Transactional
    @Override
    public MessageDto saveMessage(MessageDto messageMo) {
        MessageDto save = messageRepository.save(messageMo);
        return save;
    }
    //增加
    public void saveMessageByTemplate(MessageDto messageMo){

    }
    //删除
    @Override
    public void deleteById(Long id) {

    }
    //修改
    @Override
    public void updateMessage(MessageDto messageMo) {
    }
    //查询
    public Page<MessageDto> selectMessage(MessagePageVo messagePageVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "ctime");
        PageRequest page = PageRequest.of(messagePageVo.getPageNo()-1, messagePageVo.getPageSize(),sort);
        Query query = new Query();
        Criteria criteria = new Criteria();
//        criteria.and("nickname").is("张三");
//        criteria.and("userId").is(messagePageVo.getUserId());
//        criteria.and("msgtype").is(messagePageVo.getMsgtype());
//        query.addCriteria(criteria);
//        query.with(page);
//        List<MessageMo> messageMos = mongoTemplate.find(query, MessageMo.class);
        MessageDto messageMo = new MessageDto();
        messageMo.setUserid(messagePageVo.getUserId());
        messageMo.setMsgtype(messagePageVo.getMsgtype());
        messageMo.setMsgtype(messagePageVo.getMsgtype());
        ExampleMatcher matcher = ExampleMatcher.matching()      //创建一个匹配器
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true) //改变默认大小写忽略方式：忽略大小写
                .withMatcher("userid", ExampleMatcher.GenericPropertyMatchers.contains()) //采用“包含匹配”的方式查询
                .withMatcher("nickname",ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("msgtype",ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("msgcontent");  //忽略属性，不参与查询() ;

        Example<MessageDto> example = Example.of(messageMo,matcher);
        Page<MessageDto> all = messageRepository.findAll(example, page);
        return all;
    }
    //用户注册信息
    public void saveLogonMessage(){
        KssUser kssUser = UserThreadLocal.get();
        System.out.println("保存用户注册信息");
    }
    //用户订单消息
    public void saveOrderMessage(Order order){
        System.out.println("保存用户订单信息");
    }
}
